
import Common from './common';

export default class {
    static extends = [Common]

    setCellProps = (field, type) => {
        return (e) => {
            const value = e.target ? e.target.value : e;
            const [selectContainer, propsSetting, config] = this.models.page.get(['selectContainer', 'propsSetting', 'config', 'selectedCellMap']);
            const id = selectContainer.id;

            if (!propsSetting[id]) {
                propsSetting[id] = {};
            }

            propsSetting[id][type] = value;

            if (selectContainer['props'] === undefined) {
                selectContainer['props'] = {};
            }
            
            if (propsSetting[id]['cellIndex'] && propsSetting[id]['cellProps']) {
                if (!selectContainer['props'][field]) {
                    selectContainer['props'][field] = {};
                }

                selectContainer['props'][field][propsSetting[id]['cellIndex']] =  propsSetting[id]['cellProps'];
            }

            this.setConfigItem(config, id, 'props', selectContainer['props']);

            this.models.page.set({
                propsSetting: {...propsSetting}
            });
        }
    }

    changeStyle = (item) => {
        return (value) => {
            if (value.target) {
                value = value.target.value;
            }
            
            const [selectContainer, styleSetting, config] = this.models.page.get(['selectContainer', 'styleSetting', 'config']);

            const id = selectContainer.id;

            if (!styleSetting[id]) {
                styleSetting[id] = {};
            }

            styleSetting[id][item] = value;
            
            if ( selectContainer['style'] === undefined) {
                selectContainer['style'] = {};
            }

            selectContainer['style'][item] = value;

            this.setConfigItem(config, id, 'style', selectContainer['style']);

            this.models.page.set({
                styleSetting: {...styleSetting}
            });
        }
    }

    changeProps = (item) => {
        return (e) => {
            const [selectContainer, propsSetting, config] = this.models.page.get(['selectContainer', 'propsSetting', 'config']);

            const id = selectContainer.id;

            if (!propsSetting[id]) {
                propsSetting[id] = {};
            }

            propsSetting[id][item] = e.target.value;
            
            if (selectContainer['props'] === undefined) {
                selectContainer['props'] = {};
            }

            selectContainer['props'][item] = e.target.value;

            this.setConfigItem(config, id, 'props', selectContainer['props']);

            this.models.page.set({
                propsSetting: {...propsSetting}
            });
        }
    }
}